package com.pysoft.web.controller.safe;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.pysoft.safe.dao.entity.UserEntity;
import com.pysoft.safe.service.UserService;

@Controller
@RequestMapping("/safe/user")
public class UserController {
	@Autowired
	private UserService userService;
	@RequestMapping(value="/list.action")
	public String list() {
		return "safe/user";
	}
	
	@RequestMapping(value="/query.action")
	@ResponseBody
	public Map<String, Object> query() {
		List<UserEntity> userList = userService.query();
		int total = userService.count();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("numRows", total);
		map.put("items", userList);
		return map;
	}
	
	@RequestMapping(value="/modify.action")
	@ResponseBody
	public Map<String, Object> modify(UserEntity user) {
		userService.modifyUser(user);
		Map<String, Object> map = new HashMap<String, Object>(1);
		map.put("success", "1");
		return map;
	}
	
	@RequestMapping(value="/add.action")
	@ResponseBody
	public UserEntity add(UserEntity user) {
		String id = userService.addUser(user);
		return userService.getUser(id);
	}
	@RequestMapping(value="/get.action")
	@ResponseBody
	public UserEntity get(String id) {
		UserEntity user = userService.getUser(id);
		return user;
	}
	
	@RequestMapping(value="/delete.action")
	@ResponseBody
	public Map<String, Object> delete(@RequestParam("idList") List<String> idList) {
		userService.deleteUser(idList);
		Map<String, Object> map = new HashMap<String, Object>(1);
		map.put("success", "1");
		return map;
	}

	@RequestMapping(value="/relatingRole.action")
	@ResponseBody
	public Map<String, Object> relatingRole(@RequestParam("userIdList") List<String> userIdList,@RequestParam("roleIdList")  List<String> roleIdList) {
		userService.relatingRole(userIdList, roleIdList);
		Map<String, Object> map = new HashMap<String, Object>(1);
		map.put("success", "1");
		return map;
	}
	
	@RequestMapping(value="/cancelRelatingRole.action")
	@ResponseBody
	public Map<String, Object> cancelRelatingRole(@RequestParam("userIdList") List<String> userIdList) {
		userService.cancelRelatingRole(userIdList);
		Map<String, Object> map = new HashMap<String, Object>(1);
		map.put("success", "1");
		return map;
	}

	@RequestMapping(value="/getRelatingRole.action")
	@ResponseBody
	public List<String> getRelatingRole(@RequestParam("userId") String userId) {
		return userService.getRelatingRoles(userId);
	}
}
